//
//  MXNormalFooderController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/6.
//

#import "MXNormalFooderController.h"
#import "MxPublicNav.h"
#import "MXVisibleFooderCommV.h"
#import "MXFooderPlanController.h"
#import "MXHandleAleartV.h"
@interface MXNormalFooderController ()<MxNavigationViewDelegate>
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) UILabel *todayCountLab;
@property (nonatomic, strong) UIView *noNetView;
@property (nonatomic, strong) MXVisibleFooderCommV *addV;
@property (nonatomic, strong) MXVisibleFooderCommV *planV;
@property (nonatomic, strong) MXHandleAleartV *addAleart;
@property (nonatomic, strong) MXHandleAleartV *sayAleart;
@end

@implementation MXNormalFooderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor dc_colorWithHexString:@"#ebf4f8"];
    [self createUI];
}

- (void)createUI {
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"智能喂食器";
    self.nav.delegate = self;
    self.nav.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.nav];
    
    UIView *whiteBg = [[UIView alloc] initWithFrame:CGRectMake(15, kNavBarHeight +24, kScreenW-30, 271)];
    whiteBg.backgroundColor = [UIColor dc_colorWithHexString:@"#D1D1D1" alpha:0.9];//[UIColor dc_colorWithHexString:@"#FFFFFF" alpha:0.7];
    whiteBg.layer.cornerRadius = 6;
    whiteBg.layer.masksToBounds = YES;
    [self.view addSubview:whiteBg];
    
    UIView *videoBg = [[UIView alloc] init];
    videoBg.layer.cornerRadius = 6;
    videoBg.layer.masksToBounds = YES;
    videoBg.userInteractionEnabled = YES;
    videoBg.backgroundColor = [UIColor dc_colorWithHexString:@"#FFFFFF"];
    [whiteBg addSubview:videoBg];
    [videoBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.mas_equalTo(215);
    }];
    
    UIImageView *placeV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mx_shebei"]];
    [videoBg addSubview:placeV];
    [placeV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(videoBg);
        make.width.mas_equalTo(59);
        make.height.mas_equalTo(113);
    }];
    
    self.todayCountLab = [[UILabel alloc] init];
    [whiteBg addSubview:self.todayCountLab];
    self.todayCountLab.textColor = [UIColor dc_colorWithHexString:@"#000000" alpha:0.7];
    self.todayCountLab.font = PFRFont(11);
    NSString *countStr = @"0";
    NSString *titleStr = [NSString stringWithFormat:@"今日出粮 %@ 份",countStr];
    NSMutableAttributedString *attribut = [[NSMutableAttributedString alloc] initWithString:titleStr];
    [attribut addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:21 weight:UIFontWeightBold]} range:[titleStr rangeOfString:countStr]];
    self.todayCountLab.attributedText = attribut;
    [self.todayCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-21);
        make.left.offset(15);
    }];
    
    [self.view addSubview:self.noNetView];
    [self.noNetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(37);
        make.left.offset(15);
        make.right.offset(-15);
        make.top.mas_equalTo(whiteBg.mas_bottom).offset(16);
    }];
    
    UIView *bottomV = [[UIView alloc] init];
    bottomV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomV];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.mas_equalTo(106);
    }];
    
    [self.view addSubview:self.addAleart];
    [self.addAleart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(54);
        make.bottom.mas_equalTo(bottomV.mas_top).offset(-7);
        make.width.mas_equalTo(151);
        make.height.mas_equalTo(102);
    }];

    MJWeakSelf
    self.addV = [[MXVisibleFooderCommV alloc] init];
    [bottomV addSubview:self.addV];
    [self.addV reloadViewWithIconName:@"add_food" bottomTitle:@"立即加餐"];
    [self.addV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf tapAdd];
    }]];
    [self.addV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(100);
        make.top.offset(15);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(50);
    }];
    
    self.planV = [[MXVisibleFooderCommV alloc] init];
    [bottomV addSubview:self.planV];
    [self.planV reloadViewWithIconName:@"add_plan" bottomTitle:@"喂食计划"];
    [self.planV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf tapPlan];
    }]];
    [self.planV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-100);
        make.top.offset(15);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(50);
    }];
    
}

- (UIView *)noNetView {
    if (!_noNetView) {
        _noNetView = [[UIView alloc] init];
        _noNetView.userInteractionEnabled = YES;
        _noNetView.backgroundColor = [UIColor whiteColor];
        _noNetView.layer.cornerRadius = 6;
        _noNetView.layer.masksToBounds = YES;
        
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fooder_outline"]];
        [_noNetView addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(_noNetView);
            make.left.offset(13);
            make.width.mas_equalTo(18);
            make.height.mas_equalTo(16);
        }];
        
        UILabel *noticeLab = [[UILabel alloc] init];
        noticeLab.text = @"设备离线，请检查网络";
        [_noNetView addSubview:noticeLab];
        noticeLab.font = PFRFont(13);
        noticeLab.textColor = [UIColor dc_colorWithHexString:@"#000000" alpha:0.7];
        [noticeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(_noNetView);
            make.left.offset(40);
        }];
        
        UIImageView *right = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"add_right"]];
        [_noNetView addSubview:right];
        [right mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(8);
            make.height.mas_equalTo(14);
            make.right.offset(-17);
            make.centerY.mas_equalTo(_noNetView);
        }];
    }
    return _noNetView;
}

- (MXHandleAleartV *)addAleart {
    if (!_addAleart) {
        _addAleart = [[MXHandleAleartV alloc] init];
        _addAleart.hidden = YES;
    }
    return _addAleart;
}


-(void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tapAdd {
    self.addAleart.hidden = NO;
    self.addV.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.addV.userInteractionEnabled = YES;
        self.addAleart.hidden = YES;
    });
}


- (void)tapPlan {
    MXFooderPlanController *planVc = [[MXFooderPlanController alloc] init];
    [self.navigationController pushViewController:planVc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}
@end
